package com.hp.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class UploadController {

    public static final String UPLOAD_PATH = "D:\\upload\\";

    @RequestMapping(value = "upload",method = RequestMethod.POST)
    public String upload(MultipartFile file) throws IOException {
        File dir = new File(UPLOAD_PATH);
        if(!dir.exists()){
            dir.mkdirs();
        }
        String filename = file.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));
        filename = UUID.randomUUID().toString().replace("-","") + suffix;
        //将上传的文件保存到服务器文件中
        file.transferTo(new File(UPLOAD_PATH+filename));
        return "success";
    }
}
